﻿using MovingChameLibrary.Models;
using MovingChameLibrary.Models.Action;
using MovingChameLibrary.Models.MenuPage;
using MovingChameLibrary.ServiceManager;
using MovingChameLibrary.PersonalDataManager;
using MovingChameLibrary.TourServiceReference;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MovingChameLibrary.ViewModels
{
    public class MenuViewModel : BaseViewModel
    {
        #region Properties
        private double _userLatitude;
        private double _userLongitude;

        private OfficialTourGroup _officialTourGroup;
        private PersonalTourGroup _personalTourGroup;
        private CurrentTourGroup _currentTourGroup;
        #endregion

        public MenuViewModel(double userLatitude, double userLongitude)
        {
            this._userLatitude = userLatitude;
            this._userLongitude = userLongitude;
        }

        private ObservableCollection<object> _allGroups;
        public ObservableCollection<object> AllGroups
        {
            get
            {
                _allGroups = new ObservableCollection<object>();

                #region Current Tour Group
                if (PersonalTourData.Current.CurrentTour != null)
                {
                    _currentTourGroup = new CurrentTourGroup();

                    PersonalTourData.Current.CurrentTour.ColSpan = 6;
                    PersonalTourData.Current.CurrentTour.RowSpan = 6;
                    _currentTourGroup.Items.Add(PersonalTourData.Current.CurrentTour);

                    CurrentTourMapItem currentTourMapItem = new CurrentTourMapItem();
                    currentTourMapItem.ColSpan = 3;
                    currentTourMapItem.RowSpan = 2;
                    _currentTourGroup.Items.Add(currentTourMapItem);

                    _currentTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultCurrentTourContactId,
                        "Default Contact Title", "Default Contact Subtitle", "Assets/DarkGray.png")
                        {
                            ColSpan = 3,
                            RowSpan = 2
                        });

                    _allGroups.Add(_currentTourGroup);
                }
                #endregion

                #region Official Tour Group
                _officialTourGroup = new OfficialTourGroup();

                ServiceFactory.GetTourServiceClient().GetLatestTourAsync(this._userLatitude, this._userLongitude);
                ServiceFactory.GetTourServiceClient().GetLatestTourCompleted += MenuViewModel_GetLatestTourCompleted;

                ServiceFactory.GetTourServiceClient().GetHottestTourAsync(this._userLatitude, this._userLongitude);
                ServiceFactory.GetTourServiceClient().GetHottestTourCompleted += MenuViewModel_GetHottestTourCompleted;

                ServiceFactory.GetTourServiceClient().GetCheapestTourAsync(this._userLatitude, this._userLongitude);
                ServiceFactory.GetTourServiceClient().GetCheapestTourCompleted += MenuViewModel_GetCheapestTourCompleted;

                _officialTourGroup.Items.Add(new SearchTourItem() { ColSpan = 3, RowSpan = 3 });

                _allGroups.Add(_officialTourGroup);
                #endregion

                #region Personal Tour Group
                _personalTourGroup = new PersonalTourGroup();
                if (PersonalTourData.Current.FavouriteGroup.Items.Count != 0)
                    _personalTourGroup.Items.Add(PersonalTourData.Current.FavouriteGroup.Items.LastOrDefault());
                else
                    _personalTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultFavouriteTourId,
                        "Default Favourite Title", "Default Favourite Subtitle", "Assets/DarkGray.png"));

                if (PersonalTourData.Current.PreviousGroup.Items.Count != 0)
                    _personalTourGroup.Items.Add(PersonalTourData.Current.PreviousGroup.Items.LastOrDefault());
                else
                    _personalTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultPreviousTourId,
                        "Default Previous Title", "Default Previous Subtitle", "Assets/DarkGray.png"));

                if (PersonalTourData.Current.JoinGroup.Items.Count != 0)
                    _personalTourGroup.Items.Add(PersonalTourData.Current.JoinGroup.Items.LastOrDefault());
                else
                    _personalTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultJoinTourId,
                        "Default Confirmed Title", "Default Confirmed Subtitle", "Assets/DarkGray.png"));

                foreach (CommonItemModel tour in _personalTourGroup.Items)
                {
                    tour.ColSpan = 3;
                    tour.RowSpan = 3;
                }

                _allGroups.Add(_personalTourGroup);
                #endregion

                return this._allGroups;
            }
        }

        #region Load Official Tours Event
        private void MenuViewModel_GetCheapestTourCompleted(object sender, GetCheapestTourCompletedEventArgs e)
        {
            TourData tourData = e.Result;
            if (tourData != null)
                _officialTourGroup.Items.Add(DataConverter.ConvertToTour(tourData, null));
            else
                _officialTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultCheapestTourId, "Default Cheapest Tour Title",
                    "Default Cheapest Tour Subtitle", "Assets/DarkGray.png"));
        }

        private void MenuViewModel_GetHottestTourCompleted(object sender, GetHottestTourCompletedEventArgs e)
        {
            TourData tourData = e.Result;
            if (tourData != null)
                _officialTourGroup.Items.Add(DataConverter.ConvertToTour(tourData, null));
            else
                _officialTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultHottestTourId, "Default Hottest Tour Title",
                    "Default Hottest Tour Subtitle", "Assets/DarkGray.png"));
        }

        private void MenuViewModel_GetLatestTourCompleted(object sender, GetLatestTourCompletedEventArgs e)
        {
            TourData tourData = e.Result;
            if (tourData != null)
                _officialTourGroup.Items.Add(DataConverter.ConvertToTour(tourData, null));
            else
                _officialTourGroup.Items.Add(new DefaultMenuItem(Constants.DefaultNewestTourId, "Default Latest Tour Title",
                    "Default Latest Tour Subtitle", "Assets/DarkGray.png"));
        }
        #endregion
    }
}